package com.dfzt.myutils.travel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TravelGUI extends JFrame {
    private JSpinner goTimeSpinner;
    private JSpinner returnTimeSpinner;
    private JLabel travelResultLabel;
    private JTextField moneyField;
    private JLabel moneyResultLabel;

    public TravelGUI() {
        // 设置窗口标题
        setTitle("出差补助计算器");
        // 设置窗口大小
        setSize(500, 400);
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口居中
        setLocationRelativeTo(null);

        // 创建面板
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 2));

        // 添加组件：出差补助计算部分
        panel.add(new JLabel("火车去程时间:"));
        goTimeSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor goTimeEditor = new JSpinner.DateEditor(goTimeSpinner, "yyyy-MM-dd HH:mm:ss");
        goTimeSpinner.setEditor(goTimeEditor);
        panel.add(goTimeSpinner);

        panel.add(new JLabel("火车返程时间:"));
        returnTimeSpinner = new JSpinner(new SpinnerDateModel());
        JSpinner.DateEditor returnTimeEditor = new JSpinner.DateEditor(returnTimeSpinner, "yyyy-MM-dd HH:mm:ss");
        returnTimeSpinner.setEditor(returnTimeEditor);
        panel.add(returnTimeSpinner);

        JButton calculateTravelButton = new JButton("计算出差补助");
        panel.add(calculateTravelButton);

        travelResultLabel = new JLabel("出差补助结果：");
        panel.add(travelResultLabel);

        // 添加组件：金额转中文大写部分
        panel.add(new JLabel("金额 (double):"));
        moneyField = new JTextField();
        panel.add(moneyField);

        JButton convertMoneyButton = new JButton("金额转中文大写");
        panel.add(convertMoneyButton);

        moneyResultLabel = new JLabel("中文大写金额：");
        panel.add(moneyResultLabel);

        // 添加面板到窗口
        add(panel);

        // 添加按钮点击事件
        calculateTravelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                calculateTravelSubsidy();
            }
        });

        convertMoneyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                convertMoneyToChinese();
            }
        });
    }

    private void calculateTravelSubsidy() {
        try {
            // 获取用户选择的时间
            Date goTimeDate = (Date) goTimeSpinner.getValue();
            Date returnTimeDate = (Date) returnTimeSpinner.getValue();

            // 将Date转换为字符串
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String goTimeStr = formatter.format(goTimeDate);
            String returnTimeStr = formatter.format(returnTimeDate);

            // 调用TravelUtil中的方法计算补助天数
            double daysNum = TravelUtil.getTravelDays(goTimeStr, returnTimeStr);

            // 调用TravelUtil中的方法计算补助金额
            double subsidy = TravelUtil.getTravelSubsidy(daysNum);

            // 显示结果
            travelResultLabel.setText("出差补助结果：天数=" + daysNum + ", 金额=" + subsidy);
        } catch (Exception ex) {
            travelResultLabel.setText("输入格式错误，请重新输入！");
        }
    }

    private void convertMoneyToChinese() {
        try {
            // 获取用户输入的金额
            double money = Double.parseDouble(moneyField.getText());

            // 调用TravelUtil中的方法将金额转为中文大写
            String chineseMoney = TravelUtil.moneyToChinese(money);

            // 显示结果
            moneyResultLabel.setText("中文大写金额：" + chineseMoney);
        } catch (Exception ex) {
            moneyResultLabel.setText("输入格式错误，请重新输入！");
        }
    }

    public static void main(String[] args) {
        // 创建并显示GUI窗口
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TravelGUI().setVisible(true);
            }
        });
    }
}
